﻿using Entity;
using Microsoft.AspNetCore.Hosting;
using Reopsitory.Menus;
using Service.Menus.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Menus
{
    public class MenuService : IMenuService
    {
        private readonly IMenuRepository menuRepository;
        private readonly IWebHostEnvironment environment;

        public MenuService(IMenuRepository menuRepository, IWebHostEnvironment environment)
        {
            this.menuRepository = menuRepository;
            this.environment = environment;
        }

        public List<MenuDto> Query(int pid)
        {
            var dto = new List<MenuDto>();

            var data = menuRepository.QueryList();

            var node = data.Where(x => x.PId == pid);

            if (node.Count() == 0)
            {
                return null;
            }
            foreach (var item in node)
            {
                var dy = new MenuDto
                {
                    MenuId = item.MenuId,
                    MenuName = item.MenuName,
                    MenuUrl = item.MenuUrl,
                    MenuState = item.MenuState,
                    Child = Query(item.MenuId)
                };
                dto.Add(dy);
            }
            return dto;
        }

        public RestFull<MenuDto> QueryInfo()
        {
            RestFull<MenuDto> restFull = new RestFull<MenuDto>();

            restFull.Data = Query(0);

            return restFull;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name = "menuDto" ></ param >
        /// < returns ></ returns >
        public RestFull<MenuDto> Insert(MenuDto menuDto)
        {
            MenuInfo menu = new MenuInfo();
            menu.MenuName = menuDto.MenuName;
            menu.MenuUrl = menuDto.MenuUrl;
            menu.PId = menuDto.PId;
            menu.MenuState = true;

            menuRepository.Insert(menu);

            return new RestFull<MenuDto> { Code = 1, Message = "添加成功" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RestFull Delete(int id)
        {
            var data = menuRepository.QueryFirst(id);

            menuRepository.Delete(data.MenuId);

            return new RestFull { Code = 1, Message = "删除成功" };
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RestFull QueryFirst(int id)
        {
            var data= menuRepository.QueryFirst(id);
            return new RestFull { Data = data };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name = "menuDto" ></ param >
        /// < returns ></ returns >
        public RestFull<MenuDto> Update(MenuDto menuDto)
        {
            MenuInfo menu = new MenuInfo();
            menu.MenuId = menuDto.MenuId;
            menu.MenuName = menuDto.MenuName;
            menu.MenuUrl = menuDto.MenuUrl;
            menu.MenuState = true;

            menuRepository.Update(menu);

            return new RestFull<MenuDto> { Code = 1, Message = "修改成功" };
        }
    }
}
